import java.util.ArrayList;

public class InsertionSort implements Runnable {

	private Data globalData;
	private int start;
	private int end;
	private ArrayList<Integer> list = new ArrayList<Integer>();

	public InsertionSort(Data globalData, int start, int end) {
		this.globalData = globalData;
		this.start = start;
		this.end = end;
	}

	public void insertionSort(int[] array, int start, int end) {
		for (int index = start + 1; index <= end; index++) {
			int temporaryValue = array[index];
			int previousIndex = index - 1;
			while (previousIndex >= start
					&& temporaryValue < array[previousIndex]) {
				array[previousIndex + 1] = array[previousIndex];
				previousIndex--;
			}
			array[previousIndex + 1] = temporaryValue;
		}
		for (int index = start; index <= end; index++) {
			list.add(array[index]);
		}
	}

	public void sort(int[] o) {
		insertionSort(o, start, end);
	}

	public ArrayList<Integer> getList() {
		return list;
	}

	@Override
	public void run() {
		sort(globalData.getArray());
	}
}
